package com.platform.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;

/**
 * @author txw
 */
@Component
public class ApplicationContextHelper
        implements ApplicationContextAware {
    private static ApplicationContext appCtx;

    public static ApplicationContext getApplicationContext() {
        return appCtx;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        appCtx = applicationContext;
    }

    public static Object getBean(String beanName) {
        return appCtx.getBean(beanName);
    }

    public static <T> T getBean(String beanName, Class<T> clazz) {
        return appCtx.getBean(beanName, clazz);
    }

    public static <A extends Annotation> A  test(String beanName, Class<A> annotationType){
        return appCtx.findAnnotationOnBean(beanName,annotationType);
    }

}